import React from 'react';

import * as navigator from '@packages/lib-navigator';
import router from '@packages/lib-router';
import { addParam } from '@packages/lib-url';

import projectConfig from '../project.config.js';

import aliasConfig from './router.alias';

router.alias = aliasConfig;

router.hooks.setupShowPage.tapAsync('mini', (...args) => {
    const [url, ...otherArgv] = args;
    const path = router.urlToPath(url, aliasConfig);
    if (path && path.url) {
        return [path, ...otherArgv];
    }
    return args;
});

router.hooks.afterShowPage.tap('mini', (path, options) => {
    let url;
    if (path && path.url) {
        url = path.url;
    } else {
        console.log('path:::', path);
        url = addParam('/pages/home/index', 'path', encodeURIComponent(path));
    }

    if (options.noRecordHistory) {
        navigator.replace(url);
    } else if (options.noAnimation) {
        navigator.reload(url);
    } else {
        navigator.push(url);
    }
});

class App extends React.Component {
    appid = projectConfig.wechat.appid;

    constructor(props) {
        super(props);
    }

    onLaunch(options) {
        console.log('app onLaunch', options);
        const href = router.pathToUrl('/' + options.path);
        router.href = href;
    }

    onShow(options) {
        console.log('app onShow', options);
    }

    onHide(options) {
        console.log('app onHide', options);
    }

    render() {
        return this.props.children;
    }
}

export default App;
